<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\Custom;
use app\appv110\model\StaffLink;
use app\appv110\model\CtmCollect;
use app\appv110\model\Order;
use app\appv110\model\Yuyue;
use app\appv110\model\OrdPro;
use app\appv110\model\StaffCtm;
use app\appv110\model\Store;
use app\appv110\model\StaffNote;
use app\appv110\model\StaffBindStore;
use app\appv110\model\CtmSpecial;
use app\appv110\model\Staff;
use app\appv110\model\StoreCtm;
use app\appv110\model\MedicalAddress;
use app\appv110\model\ProvCityArea;

class CustomController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 20000  
         * 20001  
         * 20002  
         */
    }

    /*
     * 我的推广顾客
     * author xfwed
     * date 2019.11.07
     */

    public function myCtm() {
        $rule = [
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //1未消费2新客户3老客户4开通特权5粉丝6体验特权7开通i美优优
        $staff_id = $all['staff_id'];
        $store_id = input('param.store_id');
        $tab = input('param.tab');
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;

        if ($store_id) {
            $where = "(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `status` IN(4,7,8) and `store_id`=" . $store_id . " and `order_id`=o.id) > 0";
            $where1 = "(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `status` IN(4,7,8) and `store_id`=" . $store_id . " and `order_id`=y.order_id) > 0";
        } else {
            $where = [];
            $where1 = [];
        }
        //tab数量
        $noExpense = StaffCtm::alias('s')
                ->where('s.staff_id', $staff_id)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data['noExpense'] = $noExpense;
        $new = Order::alias('o')
                ->join('yuyue y','y.order_id = o.id','left')
                ->where('o.staff_id', $staff_id)
                ->where('o.status', 2)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                ->where('y.status','in','1,2,3,4,5,7,8,9,10,11')
                ->where($where)
                ->group('o.ctm_id')
                ->count();
        $data['newNum'] = $new;
        $old = Order::alias('o')
                ->where('o.staff_id', $staff_id)
                ->where('o.status', 2)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                ->where($where)
                ->group('o.ctm_id')
                ->count();
        $data['old'] = $old;
        $special1 = Order::where('staff_id', $staff_id)//本店开通
                ->where('status', 2)
                ->where('type', 4)
                ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
                ->where('mini_shop_id',0)
                ->group('ctm_id')
                ->count();
        $special2 = StaffCtm::alias('s')//逆行者活动
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                ->where('s.staff_id', $staff_id)
                ->where('c.vip_status', 2)
                ->where('cs.type',3)
                ->group('s.ctm_id')
                ->count();
        $special3 = Order::where('staff_id', $staff_id)//小店转介绍
                ->where('status', 2)
                ->where('type', 4)
                ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
                ->where('mini_shop_id','<>',0)
                ->group('ctm_id')
                ->count();
        $data['special1'] = $special1;
        $data['special2'] = $special2;
        $data['special3'] = $special3;
        $data['special'] = $special1+$special2+$special3;
        $fans = CtmCollect::where('type', 2)
                ->where('pro_id', $staff_id)
                ->count();
        $data['fans'] = $fans;
        $expSpecial = StaffCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id')
                ->where('s.staff_id', $staff_id)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->where('c.vip_status', 3)
                ->group('s.ctm_id')
                ->count();
        $data['expSpecial'] = $expSpecial;
        $smallshop = StaffCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id')
                ->where('s.staff_id', $staff_id)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->where('c.is_smallshop', 1)
                ->group('s.ctm_id')
                ->count();
        $data['smallshop'] = $smallshop;
        if ($type == 2) {
            //全部
            $all = Order::alias('o')
                    ->join('yuyue y','y.order_id = o.id','left')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->where('y.status','in','1,2,3,4,5,7,8,9,10,11')
                    ->where($where)
                    ->group('o.ctm_id')
                    ->count();
            $data['all'] = $all;
            //已付款
            $pay = Order::alias('o')
                    ->join('yuyue y','y.order_id = o.id','left')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->where('y.status','in','1,5,9,10,11')
                    ->where($where)
                    ->group('o.ctm_id')
                    ->count();
//            return Utils::ajaxReturn(0, '666', $pay);


            $data['pay'] = $pay;
            //预约中
            $yuyue = Order::alias('o')
                    ->join('yuyue y','y.order_id = o.id','left')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->where('y.status','in','2,3')
                    ->where($where)
                    ->group('o.ctm_id')
                    ->count();
            $data['yuuyue'] = $yuyue;
            //服务完成
            $fuwu = Order::alias('o')
                    ->join('yuyue y','y.order_id = o.id','left')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->where('y.status','in','4,7,8')
                    ->where($where)
                    ->group('o.ctm_id')
                    ->count();
            $data['fuwu'] = $fuwu;
        }




        //列表   1未消费2新客户3老客户4开通特权5粉丝6体验特权7开通i美优优
        if ($type == 1) {
            $noExpenseList = StaffCtm::alias('s')
                    ->where('s.staff_id', $staff_id)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('s.add_time desc')
                    ->field('s.ctm_id')
                    ->select();
            foreach ($noExpenseList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $noExpenseList[$k]['note'] = $notename['note'];
                $noExpenseList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $noExpenseList[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $noExpenseList[$k]['is_focus'] = 2;
                }
                //沟通信息
                $noExpenseList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $noExpenseList[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $noExpenseList[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $noExpenseList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $noExpenseList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $noExpenseList[$k]['is_medical'] = $is_medical;
                $noExpenseList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $noExpenseList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $noExpenseList[$k]['custom_name'] = $ctmRow['custom_name'];
                $noExpenseList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $noExpenseList[$k]['vip_status'] = $ctmRow['vip_status'];
                $noExpenseList[$k]['source_type'] = $ctmRow['source_type'];
                $noExpenseList[$k]['source_id'] = $ctmRow['source_id'];
            }
            $data['list'] = $noExpenseList;
        } elseif ($type == 2) {
            if ($tab == 1) {
                $yuyuewhere['y.status'] = ['in','1,2,3,4,5,7,8,9,10,11'];
            } elseif ($tab == 2) {
                $yuyuewhere['y.status'] = ['in','1,5,9,10,11'];
            } elseif ($tab == 3) {
                $yuyuewhere['y.status'] = ['in','2,3'];
            } elseif ($tab == 4) {
                $yuyuewhere['y.status'] = ['in','4,7,8'];
            }

            $allList = Order::alias('o')
                    ->join('yuyue y','y.order_id = o.id','left')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->where($yuyuewhere)
                    ->where($where)
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('o.add_time desc')
                    ->field('o.id,o.ctm_id')
                    ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $allList[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $allList[$k]['is_focus'] = 2;
                }
                //沟通信息
                $allList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $allList[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $allList[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $allList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $allList[$k]['is_medical'] = $is_medical;
                $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $allList[$k]['source_type'] = $ctmRow['source_type'];
                $allList[$k]['source_id'] = $ctmRow['source_id'];
                $yuyueRow = Yuyue::where('order_id', $v['id'])
                        ->order('status asc')
                        ->field('id,store_id,status,start_time')
                        ->find();
                $allList[$k]['yuyue_status'] = $yuyueRow['status'];
                $allList[$k]['yuyue_start_time'] = $yuyueRow['start_time'];
                if ($yuyueRow['store_id'] != 0) {
                    $allList[$k]['store_name'] = Store::where('id', $yuyueRow['store_id'])->value('store_name');
                }
            }
            $data['list'] = $allList;
        } elseif ($type == 3) {
            $oldList = Order::alias('o')
                    ->where('o.staff_id', $staff_id)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`=" . $staff_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                    ->where($where)
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('o.add_time desc')
                    ->field('o.id,o.ctm_id')
                    ->select();
            foreach ($oldList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $oldList[$k]['note'] = $notename['note'];
                $oldList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $oldList[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $oldList[$k]['is_focus'] = 2;
                }
                //沟通信息
                $oldList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $oldList[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $oldList[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $oldList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $oldList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $oldList[$k]['is_medical'] = $is_medical;
                $oldList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $oldList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $oldList[$k]['custom_name'] = $ctmRow['custom_name'];
                $oldList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $oldList[$k]['vip_status'] = $ctmRow['vip_status'];
                $oldList[$k]['source_type'] = $ctmRow['source_type'];
                $oldList[$k]['source_id'] = $ctmRow['source_id'];
                $yuyueRow = Yuyue::where('order_id', $v['id'])
                        ->order('status asc')
                        ->field('id,store_id,status,start_time')
                        ->find();
                $oldList[$k]['yuyue_status'] = $yuyueRow['status'];
                $oldList[$k]['yuyue_start_time'] = $yuyueRow['start_time'];
                if ($yuyueRow['store_id'] != 0) {
                    $oldList[$k]['store_name'] = Store::where('id', $yuyueRow['store_id'])->value('store_name');
                }
            }
            $data['list'] = $oldList;
        } elseif ($type == 4) {
            if($tab == 1){//1本店开通2逆行者活动3小店转介绍
                $specialList = Order::where('staff_id', $staff_id)
                    ->where('status', 2)
                    ->where('type', 4)
                    ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
                    ->where('mini_shop_id',0)
                    ->group('ctm_id')
                    ->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('id,ctm_id')
                    ->select();
            }elseif ($tab == 2) {
                $specialList = StaffCtm::alias('s')//逆行者活动
                        ->join('custom c', 'c.id = s.ctm_id','left')
                        ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                        ->where('s.staff_id', $staff_id)
                        ->where('c.vip_status', 2)
                        ->where('cs.type',3)
                        ->group('s.ctm_id')
                        ->limit($offset, $perPage)
                        ->order('s.add_time desc')
                        ->field('s.id,s.ctm_id')
                        ->select();
            }elseif ($tab == 3) {
                $specialList = Order::where('staff_id', $staff_id)
                    ->where('status', 2)
                    ->where('type', 4)
                    ->where('pay_time', '>', date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 365))
                    ->where('mini_shop_id','<>',0)
                    ->group('ctm_id')
                    ->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('id,ctm_id')
                    ->select();
            }
            
            
            foreach ($specialList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $specialList[$k]['note'] = $notename['note'];
                $specialList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $specialList[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $specialList[$k]['is_focus'] = 2;
                }
                //沟通信息
                $specialList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $specialList[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $specialList[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $specialList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $specialList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $specialList[$k]['is_medical'] = $is_medical;
                $specialList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $specialList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $specialList[$k]['custom_name'] = $ctmRow['custom_name'];
                $specialList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $specialList[$k]['vip_status'] = $ctmRow['vip_status'];
                $specialList[$k]['source_type'] = $ctmRow['source_type'];
                $specialList[$k]['source_id'] = $ctmRow['source_id'];
            }

            $data['list'] = $specialList;
        } elseif ($type == 5) {
            $fansList = CtmCollect::where('type', 2)
                    ->where('pro_id', $staff_id)
                    ->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('id,ctm_id')
                    ->select();
            foreach ($fansList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $fansList[$k]['note'] = $notename['note'];
                $fansList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $fansList[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $fansList[$k]['is_focus'] = 2;
                }
                //沟通信息
                $fansList[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $fansList[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $fansList[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $fansList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $fansList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $fansList[$k]['is_medical'] = $is_medical;
                $fansList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $fansList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $fansList[$k]['custom_name'] = $ctmRow['custom_name'];
                $fansList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $fansList[$k]['vip_status'] = $ctmRow['vip_status'];
                $fansList[$k]['source_type'] = $ctmRow['source_type'];
                $fansList[$k]['source_id'] = $ctmRow['source_id'];
            }
            $data['list'] = $fansList;
        } elseif ($type == 6) {
            $expSpecial = StaffCtm::alias('s')
                    ->join('custom c', 'c.id = s.ctm_id')
                    ->where('c.vip_status', 3)
                    ->where('s.staff_id', $staff_id)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('s.add_time desc')
                    ->field('s.ctm_id')
                    ->select();
            foreach ($expSpecial as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $expSpecial[$k]['note'] = $notename['note'];
                $expSpecial[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $expSpecial[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $expSpecial[$k]['is_focus'] = 2;
                }
                //沟通信息
                $expSpecial[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $expSpecial[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $expSpecial[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $expSpecial[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $expSpecial[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $expSpecial[$k]['is_medical'] = $is_medical;
                $expSpecial[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $expSpecial[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $expSpecial[$k]['custom_name'] = $ctmRow['custom_name'];
                $expSpecial[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $expSpecial[$k]['vip_status'] = $ctmRow['vip_status'];
                $expSpecial[$k]['source_type'] = $ctmRow['source_type'];
                $expSpecial[$k]['source_id'] = $ctmRow['source_id'];
            }
            $data['list'] = $expSpecial;
        }elseif ($type == 7) {
            $smallshop = StaffCtm::alias('s')
                    ->join('custom c', 'c.id = s.ctm_id')
                    ->where('c.is_smallshop', 1)
                    ->where('s.staff_id', $staff_id)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('s.add_time desc')
                    ->field('s.ctm_id')
                    ->select();
            foreach ($smallshop as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $smallshop[$k]['note'] = $notename['note'];
                $smallshop[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $smallshop[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $smallshop[$k]['is_focus'] = 2;
                }
                //沟通信息
                $smallshop[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $smallshop[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $smallshop[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $smallshop[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $smallshop[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $smallshop[$k]['is_medical'] = $is_medical;
                $smallshop[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $smallshop[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $smallshop[$k]['custom_name'] = $ctmRow['custom_name'];
                $smallshop[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $smallshop[$k]['vip_status'] = $ctmRow['vip_status'];
                $smallshop[$k]['source_type'] = $ctmRow['source_type'];
                $smallshop[$k]['source_id'] = $ctmRow['source_id'];
            }
            $data['list'] = $smallshop;
        }
        foreach ($data['list'] as $k => $v){
            if($v['source_type'] == 0){
                $sourceName = '';
            }elseif ($v['source_type'] == 1) {
                $sourceName = '平台引流';
            }elseif ($v['source_type'] == 2) {
                $staffName = Staff::where('id',$v['source_id'])->value('staff_name');
                $sourceName = '美容师'.$staffName;
            }elseif ($v['source_type'] == 3) {
                $StoreRow = Store::where('id',$v['source_id'])->field('store_name,city')->find();
                $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                $sourceName = $cityName.'的'.$StoreRow['store_name'];
            }elseif ($v['source_type'] == 4) {
                $sourceName = '小店转介绍';
            }
            $data['list'][$k]['sourceName'] = $sourceName;
            //查询续费年数
            $specialNum = CtmSpecial::where('ctm_id',$v['ctm_id'])->count();
            $data['list'][$k]['renewYears'] = $specialNum;
        }
        return Utils::ajaxReturn(0, '成功', $data);
    }

    /*
     * 用户详情
     * author xfwed
     * date 2019.11.07
     */

    public function ctmInfo() {
        $rule = [
            'staff_id' => 'require',
            'ctm_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $staff_id = $all['staff_id'];
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        //用户基本信息
        $ctmRow = Custom::where('id', $ctm_id)->field('id ctm_id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,exclusive_staff_id,exclusive_time,warning_level')->find();
        $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['ctm_id'])->field('name')->find();
        if ($notename['name']) {
            $ctmRow['custom_name'] = $notename['name'];
        }
        if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
            $specialTime = CtmSpecial::where('ctm_id', $ctmRow['ctm_id'])->order('id asc')->find();
            $ctmRow['specialTime'] = $specialTime['add_time'];
            if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['ctm_id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
        if($ctmRow['source'] == 1){
            $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
        }            
        } else {
            $ctmRow['specialTime'] = NULL;
        }
        $data['ctmInfo'] = $ctmRow;
        //用户是否关注了美容师
        $ctmCollectRow = CtmCollect::where('ctm_id', $ctm_id)
                ->where('type', 2)
                ->where('pro_id', $staff_id)
                ->find();
        if ($ctmCollectRow) {
            $data['is_focus'] = 1; //1是2否关注了我
        } else {
            $data['is_focus'] = 2;
        }
        //沟通信息
        $data['linkNum'] = StaffLink::where('staff_id', $staff_id)
                ->where('ctm_id', $ctm_id)
                ->count();
        $data['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                ->where('ctm_id', $ctm_id)
                ->order('add_time desc')
                ->value('add_time');

        //是否是通过美容师开通的特权
        if ($ctmRow['exclusive_staff_id'] == $staff_id) {
            $data['special'] = 1; //1是2否通过我购买的
        } else {
            $data['special'] = 2;
        }

        //购买项目总数
        $data['itemNum'] = Yuyue::alias('y')
                ->join('order o', 'o.id= y.order_id', 'left')
                ->where('y.ctm_id', $ctm_id)
                ->where('o.staff_id', $staff_id)
                ->where('y.status', '<>', 6)
                ->where('o.type', '<>', 3)
                ->count();
        //待预约
        $data['itemNum1'] = Yuyue::alias('y')
                ->join('order o', 'o.id= y.order_id', 'left')
                ->where('y.ctm_id', $ctm_id)
                ->where('o.staff_id', $staff_id)
                ->where('y.status', 'in', '1,5,9,10,11')
                ->count();
        //待确认
        $data['itemNum2'] = Yuyue::alias('y')
                ->join('order o', 'o.id= y.order_id', 'left')
                ->where('y.ctm_id', $ctm_id)
                ->where('o.staff_id', $staff_id)
                ->where('y.status', 'in', '2')
                ->count();
        //待服务
        $data['itemNum3'] = Yuyue::alias('y')
                ->join('order o', 'o.id= y.order_id', 'left')
                ->where('y.ctm_id', $ctm_id)
                ->where('o.staff_id', $staff_id)
                ->where('y.status', 'in', '3')
                ->count();
        //已服务
        $data['itemNum4'] = Yuyue::alias('y')
                ->join('order o', 'o.id= y.order_id', 'left')
                ->where('y.ctm_id', $ctm_id)
                ->where('o.staff_id', $staff_id)
                ->where('y.status', 'in', '4,7,8')
                ->count();
        //项目订单
        $orderdata = Order::where('ctm_id', $ctm_id)
                ->where('staff_id', $staff_id)
                ->where('status', 2)
                ->where('type', '<>', 3)
                ->field('id,type,ord_num,pay_time')
                ->order('pay_time desc')
                ->limit($offset, $perPage)
                ->select();
        foreach ($orderdata as $k => $v) {
            if ($v['type'] == 1) {
                $item_name = OrdPro::alias('o')
                        ->join('item i', 'i.id = o.pro_id', 'left')
                        ->where('o.ord_id', '=', $v['id'])
                        ->value('i.item_name');
            } elseif ($v['type'] == 2) {
                $item_name = OrdPro::alias('o')
                        ->join('overgroup g', 'g.id = o.pro_id', 'left')
                        ->where('o.ord_id', '=', $v['id'])
                        ->value('g.name');
            } elseif ($v['type'] == 4) {
                $item_name = '';
            } elseif ($v['type'] == 5) {
                $item_name = OrdPro::alias('o')
                        ->join('miaosha m', 'm.id = o.pro_id', 'left')
                        ->join('item i', 'i.id = m.item_id', 'left')
                        ->where('o.ord_id', '=', $v['id'])
                        ->value('i.item_name');
            } elseif ($v['type'] == 6) {
                $item_name = '';
            }elseif ($v['type'] == 7) {
                $item_name = Db::name('order_yimei')->alias('o')
                        ->join('item i', 'i.id = o.item_id', 'left')
                        ->where('o.ord_id', '=', $v['id'])
                        ->value('i.item_name');
            }elseif ($v['type'] == 8) {
                $item_name = OrdPro::alias('o')
                        ->join('overgroup i', 'i.id = o.pro_id', 'left')
                        ->where('o.ord_id', '=', $v['id'])
                        ->value('i.name');
            }
            $orderdata[$k]['item_name'] = $item_name;
        }
        $data['orderList'] = $orderdata;
        //查询用户是否为逆行者
        $medicalData = MedicalAddress::where('ctm_id',$ctm_id)->count();
        if($medicalData > 0){
            $is_medical = 1;
        }  else {
            $is_medical = 2;
        }
        $data['ctmInfo']['is_medical'] = $is_medical;
        //查询续费年数
        $specialNum = CtmSpecial::where('ctm_id',$ctm_id)->count();
        $data['ctmInfo']['renewYears'] = $specialNum;
        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 用户搜索(美容师端)
     * author xfwed
     * date 2019.11.08
     */

    public function searchCtm() {
        $rule = [
            'staff_id' => 'require',
            'keyword' => 'require',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
            'keyword.require' => '关键字必须数字',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id'];
        $keyword = $all['keyword'];
        $list = StaffCtm::alias('sc')
                ->join('custom c', 'c.id = sc.ctm_id', 'left')
                ->where('sc.staff_id', $staff_id)
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                ->group('sc.ctm_id')
                ->select();
        if (empty($list)) {
            $staffCtmList = StaffCtm::where('staff_id', $staff_id)->group('ctm_id')->select();
            $list = [];
            foreach ($staffCtmList as $k => $v) {
                $one = StaffNote::alias('sc')
                        ->join('custom c', 'c.id = sc.ctm_id', 'left')
                        ->where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('name', 'like', '%' . $keyword . '%')
                        ->field('c.id,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                        ->select();
                if ($one) {
                    $list = array_merge($list, $one);
                }
            }
        }
        $collectList = CtmCollect::alias('cc')
                ->join('custom c', 'c.id = cc.ctm_id', 'left')
                ->where('cc.type', 2)
                ->where('cc.pro_id', $staff_id)
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                ->select();
        if (empty($collectList)) {
            $staffCtmList = CtmCollect::where('type', 2)->where('pro_id', $staff_id)->select();
            $collectList = [];
            foreach ($staffCtmList as $k => $v) {
                $one = StaffNote::alias('sc')
                        ->join('custom c', 'c.id = sc.ctm_id', 'left')
                        ->where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('name', 'like', '%' . $keyword . '%')
                        ->field('c.id,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                        ->select();
                if ($one) {
                    $collectList = array_merge($collectList, $one);
                }
            }
        }
        
        //1专属特权
        $speciallist = Order::alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->where('o.staff_id', $staff_id)//专属特权
                ->where('o.status', 2)
                ->where('o.type', 4)
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                ->select();
        //2推广顾客
        $spreadlist = Custom::where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->where('custom_name', 'like', '%' . $keyword . '%')
                ->field('id,source,custom_name,custom_photo,vip_status,custom_phone,is_smallshop,source_type,source_id,warning_level')
                ->select();
        //5其他(服务完成的客户)
        $otherlist = Yuyue::alias('y')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->where('y.staff_id',$staff_id)
                ->where('y.status','in','4,7,8')
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                ->select();

        $data = array_merge($list, $collectList,$speciallist,$spreadlist,$otherlist);
//        return Utils::ajaxReturn(0, '查询成功', $data);
        if (count($data) > 0) {
            $tmp_arr = array();
            foreach ($data as $k => $v) {
                if (in_array($v['id'], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                    unset($data[$k]);
                } else {
                    $tmp_arr[] = $v['id'];
                }
            }
            sort($data); //sort函数对数组进行排序
//            return Utils::ajaxReturn(6666, '666', $data);
            foreach ($data as $k => $v) {
                $data[$k]['ctm_id'] = $v['id'];
                $ctm_id = $v['id'];
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $ctm_id)
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $data[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $data[$k]['is_focus'] = 2;
                }
                //沟通信息
                $data[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $ctm_id)
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $ctm_id)
                        ->where('status', 2)
                        ->where('staff_id', $staff_id)
                        ->find();
                if ($orderRow) {
                    $data[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $data[$k]['special'] = 2;
                }
                
                //查询用户开通特权时间
                if ($v['vip_status'] == 2 || $v['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctm_id)->order('id asc')->find();
                    $data[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $data[$k]['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctm_id)
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $data[$k]['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($v['source'] == 1){
                        $data[$k]['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $data[$k]['specialTime'] = NULL;
                }

                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $v['id'])->field('note,name')->find();
                $data[$k]['note'] = $notename['note'];
                $data[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $data[$k]['custom_name'] = $notename['name'];
                }
                
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctm_id)->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $data[$k]['is_medical'] = $is_medical;
                
                if($v['source_type'] == 0){
                    $sourceName = '';
                }elseif ($v['source_type'] == 1) {
                    $sourceName = '平台引流';
                }elseif ($v['source_type'] == 2) {
                    $staffName = Staff::where('id',$v['source_id'])->value('staff_name');
                    $sourceName = '美容师'.$staffName;
                }elseif ($v['source_type'] == 3) {
                    $StoreRow = Store::where('id',$v['source_id'])->field('store_name,city')->find();
                    $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                    $sourceName = $cityName.'的'.$StoreRow['store_name'];
                }elseif ($v['source_type'] == 4) {
                    $sourceName = '小店转介绍';
                }
                $data[$k]['sourceName'] = $sourceName;
                //查询续费年数
            $specialNum = CtmSpecial::where('ctm_id',$ctm_id)->count();
            $data[$k]['renewYears'] = $specialNum;
                }
        }



        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 本店客户（上线以后 作废 改为newStoreCtm）
     * author xfwed
     * date 2019.11.12
     */

    public function storeCtm() {
        $rule = [
            'store_id' => 'require',
            'type' => 'require',
            'staff_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //0全部 1普通 299特权
        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];
        $where['y.store_id'] = $store_id;
        if ($type == 1) {
            $where['c.vip_status'] = ['<>', 2];
        } elseif ($type == 2) {
            $where['c.vip_status'] = 2;
        }
        if ($staff_id == 0) {
            $where['o.staff_id'] = 0;
        } elseif ($staff_id > 0) {
            $where['o.staff_id'] = $staff_id;
        }

        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        $num = Yuyue::alias('y')
                ->join('order o', 'o.id = y.order_id', 'left')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->join('staff s', 's.id = o.staff_id', 'left')
                ->where($where)
                ->group('y.ctm_id')
                ->count();
        $data['num'] = $num;
        $list = Yuyue::alias('y')
                ->join('order o', 'o.id = y.order_id', 'left')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->join('staff s', 's.id = o.staff_id', 'left')
                ->where($where)
                ->field('c.id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,o.pay_time,s.staff_name')
                ->limit($offset, $perPage)
                ->group('y.ctm_id')
                ->select();
        $data['list'] = $list;
        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 本店美容师（筛选用）
     * author xfwed
     * date 2019.11.12
     */

    public function storeStaff() {
        $rule = [
            'store_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $store_id = $all['store_id'];
        $stafflist = StaffBindStore::alias('b')
                ->join('staff s', 's.id = b.staff_id', 'left')
                ->where('b.store_id', $store_id)
                ->field('b.staff_id,s.staff_name')
                ->select();
        $list[0]['staff_id'] = 0;
        $list[0]['staff_name'] = '全部推广';
        $list[1]['staff_id'] = -1;
        $list[1]['staff_name'] = '逆行者活动';
        $list[2]['staff_id'] = -2;
        $list[2]['staff_name'] = '店铺推广';
        $list[3]['staff_id'] = -3;
        $list[3]['staff_name'] = '活动推广';
        foreach ($stafflist as $k => $v) {
            $list[$k + 4]['staff_id'] = $v['staff_id'];
            $list[$k + 4]['staff_name'] = $v['staff_name'];
        }
        return Utils::ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 客户分类（筛选用）(作废)
     * author xfwed
     * date 2019.11.26
     */

    public function storeCtmType() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        //1未消费2新客户3老客户4开通特权5体验特权
        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];

        if ($staff_id) {
            $staff_ids = $staff_id;
        } else {
            $staff_ids = StaffBindStore::where('store_id', $store_id)->column('staff_id');
            $staff_ids = implode(",", $staff_ids);
        }
        $allList = StaffCtm::alias('s')
                ->join('staff st', 'st.id = s.staff_id', 'left')
                ->where('s.staff_id', 'in', $staff_ids)
                //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[0]['id'] = 0;
        $data[0]['name'] = '全部（'.$allList.'）';
        //tab数量
        $noExpense = StaffCtm::alias('s')
                ->where('s.staff_id', 'in', $staff_ids)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[1]['id'] = 1;
        $data[1]['name'] = '访客（' . $noExpense . '）';
        $new = Order::alias('o')
                ->where('o.staff_id', 'in', $staff_ids)
                ->where('o.status', 2)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                ->group('o.ctm_id')
                ->count();
        $data[2]['id'] = 2;
        $data[2]['name'] = '初次消费（' . $new . '）';
        $old = Order::alias('o')
                ->where('o.staff_id', 'in', $staff_ids)
                ->where('o.status', 2)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                ->group('o.ctm_id')
                ->count();
        $data[3]['id'] = 3;
        $data[3]['name'] = '老客户（' . $old . '）';
        $special = Order::where('staff_id', 'in', $staff_ids)
                ->where('status', 2)
                ->where('type', 4)
                ->group('ctm_id')
                ->count();
        $data[4]['id'] = 4;
        $data[4]['name'] = '特权用户（' . $special . '）';
        $expSpecial = StaffCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id')
                ->where('c.vip_status', 3)
                ->where('s.staff_id', 'in', $staff_ids)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->group('s.ctm_id')
                ->count();
        $data[5]['id'] = 5;
        $data[5]['name'] = '体验特权（' . $expSpecial . '）';
        $ctm_collect = CtmCollect::alias('co')
                ->join('custom c', 'c.id = co.ctm_id', 'left')
                ->where('pro_id', $store_id)
                ->where('type', 3)
                ->count();
        $data[6]['id'] = 6;
        $data[6]['name'] = '收藏本店（' . $ctm_collect . '）';
        return Utils::ajaxReturn(0, '成功', $data);
    }

    /*
     * 本店客户（作废）
     * author xfwed
     * date 2019.11.26
     */

    public function newStoreCtm() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //1未消费2新客户3老客户4开通特权5体验特权
        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;

        if ($staff_id) {
            $staff_ids = $staff_id;
        } else {
            $staff_ids = StaffBindStore::where('store_id', $store_id)->column('staff_id');
            $staff_ids = implode(",", $staff_ids);
        }
        //列表   1未消费2新客户3老客户4开通特权5体验特权
        if ($type == 0) {
            $allList = StaffCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->where('s.staff_id', 'in', $staff_ids)
                    //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('s.ctm_id,st.staff_name,st.staff_on')
                    ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $allList[$k]['specialTime'] = $specialTime;
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('pay_money', '>', 0)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $allList[$k]['pay_time'] = $pay_time;
            }
            $data = $allList;
        } elseif ($type == 1) {
            $noExpenseList = StaffCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->where('s.staff_id', 'in', $staff_ids)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('s.ctm_id,st.staff_name,st.staff_on,st.add_time')
                    ->select();
            foreach ($noExpenseList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $noExpenseList[$k]['note'] = $notename['note'];
                $noExpenseList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $noExpenseList[$k]['specialTime'] = $specialTime;
                } else {
                    $noExpenseList[$k]['specialTime'] = NULL;
                }
                $noExpenseList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $noExpenseList[$k]['custom_name'] = $ctmRow['custom_name'];
                $noExpenseList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $noExpenseList[$k]['vip_status'] = $ctmRow['vip_status'];
                $noExpenseList[$k]['pay_time'] = null;
            }
            $data = $noExpenseList;
        } elseif ($type == 2) {
            $allList = Order::alias('o')
                    ->join('staff st', 'st.id = o.staff_id', 'left')
                    ->where('o.staff_id', 'in', $staff_ids)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                    ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }

                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $allList[$k]['specialTime'] = $specialTime;
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('pay_money', '>', 0)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $allList[$k]['pay_time'] = $pay_time;
            }
            $data = $allList;
        } elseif ($type == 3) {
            $oldList = Order::alias('o')
                    ->join('staff st', 'st.id = o.staff_id', 'left')
                    ->where('o.staff_id', 'in', $staff_ids)
                    ->where('o.status', 2)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                    ->select();
            foreach ($oldList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $oldList[$k]['note'] = $notename['note'];
                $oldList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $oldList[$k]['specialTime'] = $specialTime;
                } else {
                    $oldList[$k]['specialTime'] = NULL;
                }
                $oldList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $oldList[$k]['custom_name'] = $ctmRow['custom_name'];
                $oldList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $oldList[$k]['vip_status'] = $ctmRow['vip_status'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('pay_money', '>', 0)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $oldList[$k]['pay_time'] = $pay_time;
            }
            $data = $oldList;
        } elseif ($type == 4) {
            $specialList = Order::alias('o')
                    ->join('staff st', 'st.id = o.staff_id', 'left')
                    ->where('o.staff_id', 'in', $staff_ids)
                    ->where('o.status', 2)
                    ->where('o.type', 4)
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                    ->select();
            foreach ($specialList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $specialList[$k]['note'] = $notename['note'];
                $specialList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $specialList[$k]['specialTime'] = $specialTime;
                } else {
                    $specialList[$k]['specialTime'] = NULL;
                }
                $specialList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $specialList[$k]['custom_name'] = $ctmRow['custom_name'];
                $specialList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $specialList[$k]['vip_status'] = $ctmRow['vip_status'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('pay_money', '>', 0)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $specialList[$k]['pay_time'] = $pay_time;
            }

            $data = $specialList;
        } elseif ($type == 5) {
            $expSpecial = StaffCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->join('custom c', 'c.id = s.ctm_id')
                    ->where('c.vip_status', 3)
                    ->where('s.staff_id', 'in', $staff_ids)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->field('s.ctm_id,st.staff_name,st.staff_on')
                    ->select();

            foreach ($expSpecial as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $expSpecial[$k]['note'] = $notename['note'];
                $expSpecial[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $expSpecial[$k]['specialTime'] = $specialTime;
                } else {
                    $expSpecial[$k]['specialTime'] = NULL;
                }
                $expSpecial[$k]['expspecial_time'] = $specialTime;
                $expSpecial[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $expSpecial[$k]['custom_name'] = $ctmRow['custom_name'];
                $expSpecial[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $expSpecial[$k]['vip_status'] = $ctmRow['vip_status'];
                $expSpecial[$k]['pay_time'] = null;
            }
            $data = $expSpecial;
        } elseif ($type == 6) {
            $ctm_collect = CtmCollect::alias('co')
                    ->join('custom c', 'c.id = co.ctm_id', 'left')
                    ->where('pro_id', $store_id)
                    ->where('type', 3)
                    ->limit($offset, $perPage)
                    ->field('co.ctm_id,co.add_time collect_time')
                    ->select();

            foreach ($ctm_collect as $k => $v) {
                $staffId = StaffCtm::where('ctm_id', $v['ctm_id'])->value('staff_id');
                $ctm_collect[$k]['staff_name'] = Staff::where('id', $staffId)->value('staff_name');
                $ctm_collect[$k]['staff_on'] = Staff::where('id', $staffId)->value('staff_on');
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,custom_phone,custom_name,custom_photo,vip_status')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $ctm_collect[$k]['note'] = $notename['note'];
                $ctm_collect[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->value('add_time');
                    $ctm_collect[$k]['specialTime'] = $specialTime;
                } else {
                    $ctm_collect[$k]['specialTime'] = NULL;
                }
                $ctm_collect[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $ctm_collect[$k]['custom_name'] = $ctmRow['custom_name'];
                $ctm_collect[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $ctm_collect[$k]['vip_status'] = $ctmRow['vip_status'];
                $ctm_collect[$k]['pay_time'] = null;
            }
            $data = $ctm_collect;
        }
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 用户搜索(老板端)
     * author xfwed
     * date 2019.12.23
     */

    public function bossSearchCtm() {
        $rule = [
            'store_id' => 'require',
            'keyword' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'keyword.require' => '关键字必须数字',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $store_id = $all['store_id'];
        $keyword = $all['keyword'];
        
//        $staff_ids = StaffBindStore::where('store_id', $store_id)->column('staff_id');
//        $staff_ids = implode(",", $staff_ids);
        $list = StoreCtm::alias('sc')
                ->join('custom c', 'c.id = sc.ctm_id', 'left')
                ->join('staff s','s.id=sc.staff_id','left')
                ->where('sc.store_id','in', $store_id)
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,s.staff_name,s.staff_on,c.warning_level')
                ->group('sc.ctm_id')
                ->select();
        if (empty($list)) {
            $staffCtmList = StoreCtm::where('store_id', $store_id)->group('ctm_id')->select();
            $list = [];
            foreach ($staffCtmList as $k => $v) {
                $one = Db::name('store_note')->alias('sc')
                        ->join('custom c', 'c.id = sc.ctm_id', 'left')
                        ->where('sc.store_id', $store_id)
                        ->where('sc.ctm_id', $v['ctm_id'])
                        ->where('sc.name', 'like', '%' . $keyword . '%')
                        ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                        ->select();
                
                if ($one) {
                    $staffRow = Db::name('staff')->where('id',$v['staff_id'])->field('staff_name,staff_on')->find();
                    $one[0]['staff_name'] = $staffRow['staff_name'];
                    $one[0]['staff_on'] = $staffRow['staff_on'];
                    $list = array_merge($list, $one);
                }
            }
        }
        
        
        
        $collectList = CtmCollect::alias('cc')
                ->join('custom c', 'c.id = cc.ctm_id', 'left')
                ->where('cc.type', 3)
                ->where('cc.pro_id','=', $store_id)
                ->where('c.custom_name', 'like', '%' . $keyword . '%')
                ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                ->select();
        if (empty($collectList)) {
            $staffCtmList = CtmCollect::where('type', 3)->where('pro_id', $store_id)->select();
            $collectList = [];
            foreach ($staffCtmList as $k => $v) {
                $one = Db::name('store_note')->alias('sc')
                        ->join('custom c', 'c.id = sc.ctm_id', 'left')
                        ->where('store_id', $store_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('name', 'like', '%' . $keyword . '%')
                        ->field('c.id,c.source,c.custom_name,c.custom_photo,c.vip_status,c.custom_phone,c.is_smallshop,c.source_type,c.source_id,c.warning_level')
                        ->select();
                if ($one) {
                    $collectList = array_merge($collectList, $one);
                }
            }
        }
        foreach ($collectList as $k=>$v){
            $staffId = StoreCtm::where('ctm_id', $v['id'])->value('staff_id');
            $collectList[$k]['staff_name'] = Staff::where('id', $staffId)->value('staff_name');
            $collectList[$k]['staff_on'] = Staff::where('id', $staffId)->value('staff_on');
        }

        $data = array_merge($list, $collectList);
//        return Utils::ajaxReturn(0, '查询成功', $data);
        if (count($data) > 0) {
            $tmp_arr = array();
            foreach ($data as $k => $v) {
                if (in_array($v['id'], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                    unset($data[$k]);
                } else {
                    $tmp_arr[] = $v['id'];
                }
            }
            sort($data); //sort函数对数组进行排序

            foreach ($data as $k => $v) {
                $data[$k]['ctm_id'] = $v['id'];
                $ctm_id = $v['id'];
               //查询用户开通特权时间
                if ($v['vip_status'] == 2 || $v['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $v['id'])->order('id asc')->find();
                    $data[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $data[$k]['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctm_id)
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $data[$k]['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    
                if($v['source'] == 1){
                    $data[$k]['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                }
                } else {
                    $data[$k]['specialTime'] = NULL;
                }
                
                $pay_time = Order::where('ctm_id', $v['id'])
                        ->where('status', 2)
                        ->where('pay_money', '>', 0)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $data[$k]['pay_time'] = $pay_time;
                
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$v['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $data[$k]['is_medical'] = $is_medical;
                
                if($v['source_type'] == 0){
                    $sourceName = '';
                }elseif ($v['source_type'] == 1) {
                    $sourceName = '平台引流';
                }elseif ($v['source_type'] == 2) {
                    $staffName = Staff::where('id',$v['source_id'])->value('staff_name');
                    $sourceName = '美容师'.$staffName;
                }elseif ($v['source_type'] == 3) {
                    $StoreRow = Store::where('id',$v['source_id'])->field('store_name,city')->find();
                    $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                    $sourceName = $cityName.'的'.$StoreRow['store_name'];
                }elseif ($v['source_type'] == 4) {
                    $sourceName = '小店转介绍';
                }
                $data[$k]['sourceName'] = $sourceName;
                //查询用户是否在本店开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['id'])
                        ->where('status', 2)
                        ->where('store_id', $store_id)
                        ->order('id desc')
                        ->find();
                if ($orderRow) {
                    $data[$k]['is_exclusive'] = 1; //1是2否绑定的该店铺  显示 专属特权
                } else {
                    $data[$k]['is_exclusive'] = 2;
                }
                //查询续费年数
            $specialNum = CtmSpecial::where('ctm_id',$v['id'])->count();
            $data[$k]['renewYears'] = $specialNum;
            
            $notename = Db::name('store_note')
                    ->where('store_id', $store_id)
                    ->where('ctm_id', $v['id'])
                    ->field('note,name')
                    ->find();
            $data[$k]['note'] = $notename['note'];
            $data[$k]['note_name'] = $notename['name'];
            if ($notename['name']) {
                $data[$k]['custom_name'] = $notename['name'];
            }

            }
            
        }
        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 客户分类（筛选用）
     * author xfwed
     * date 2020.03.25
     */

    public function storeCtmType2020() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        //0全部1访客2未预约3新客户4老客户5开通特权6体验特权7收藏本店
        $staff_id = $all['staff_id'];//0全部-1店铺分享 
        $store_id = $all['store_id'];

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
              $a7 = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= c.id) > 0";
              $o = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= o.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
              $orderwhere['o.staff_id'] = 0;
          }elseif($staff_id == -3){//活动推广
              $where['s.type'] = 8;
              $orderwhere['s.type'] = 8;
              $owhere['s.type'] = 8;
          }  else {
              $where['s.staff_id'] = $staff_id;
              $orderwhere['o.staff_id'] = $staff_id;
          }    
        } 
        $orderwhere = isset($orderwhere)?$orderwhere:[];
        $a = isset($a) ? $a : '';
        $a7 = isset($a7) ? $a7 : '';
        $o = isset($o) ? $o : '';
        $owhere = isset($owhere) ? $owhere : [];
        $where['s.store_id'] = $store_id;
        $allList = StoreCtm::alias('s')
                ->join('staff st', 'st.id = s.staff_id', 'left')
                ->where($where)
                ->where($a)
                //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[0]['id'] = 0;
        $data[0]['name'] = '全部('.$allList.')';
        //tab数量
        $noExpense = StoreCtm::alias('s')
                ->where($where)
                ->where($a)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->count();
        $data[1]['id'] = 1;
        $data[1]['name'] = '访客(' . $noExpense . ')';
        $noyuyyue = StoreCtm::alias('s')
                ->where($where)
                ->where($a)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) > 0")
                ->where("(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `store_id`=".$store_id." and `status`>1 and `ctm_id`=s.ctm_id)=0")
                ->group('s.ctm_id')
                ->count();
        $data[2]['id'] = 2;
        $data[2]['name'] = '未预约(' . $noyuyyue . ')';
        $new = Order::alias('o')
                ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where($o)
                ->where($orderwhere)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                ->group('o.ctm_id')
                ->count();
        $data[3]['id'] = 3;
        $data[3]['name'] = '初次消费(' . $new . ')';
        $old = Order::alias('o')
                ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where($orderwhere)
                ->where($o)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                ->group('o.ctm_id')
                ->count();
        $data[4]['id'] = 4;
        $data[4]['name'] = '老客户(' . $old . ')';
        $special1 = Order::alias('o')//本店开通
                ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where('o.type', 4)
                ->where('o.mini_shop_id',0)
                ->where($o)
                ->where($orderwhere)
                ->group('o.ctm_id')
                ->count();
        $special2 = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('cs.type',3)
                ->where($where)
                ->where($a)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->group('s.ctm_id')
                ->count();
        $special3 = Order::alias('o')//小店转介绍
                ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                ->where('o.store_id', '=', $store_id)
                ->where('o.status', 2)
                ->where('o.type', 4)
                ->where('o.mini_shop_id','<>',0)
                ->where($orderwhere)
                ->where($o)
                ->group('o.ctm_id')
                ->count();
        $special4 = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('c.source',1)
                ->where($where)
                ->group('s.ctm_id')
                ->count();
        $special = $special1+$special2+$special3+$special4;
        $data[5]['id'] = 5;
        $data[5]['name'] = '特权用户(' . $special . ')';
        $expSpecial = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id')
                ->where('c.vip_status', 3)
                ->where($where)
                ->where($a)
                //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                ->group('s.ctm_id')
                ->count();
        $data[6]['id'] = 6;
        $data[6]['name'] = '体验特权(' . $expSpecial . ')';
        $smallshop = Custom::alias('c')
                ->join('store_ctm s','s.ctm_id = c.id and s.store_id = '.$store_id,'left')
                ->where('c.smallshop_storeid',$store_id)
                ->where('c.status', 2)
                ->where($a7)
                ->where($owhere)
                ->count();
        $data[7]['id'] = 7;
        $data[7]['name'] = '开通i美优优(' . $smallshop . ')';
        $ctm_collect = CtmCollect::alias('co')
                ->join('custom c', 'c.id = co.ctm_id', 'left')
                ->join('store_ctm s','s.ctm_id = c.id and s.store_id = '.$store_id,'left')
                ->where('co.pro_id', $store_id)
                ->where('co.type', 3)
                ->where($a7)
                ->where($owhere)
                ->count();
        $data[8]['id'] = 8;
        $data[8]['name'] = '收藏本店(' . $ctm_collect . ')';
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 本店客户
     * author xfwed
     * date 2020.03.25
     */

    public function newStoreCtm2020() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //0全部1访客2未预约3新客户4老客户5开通特权6体验特权7开通i美优优8收藏本店
        $specialType = input('param.specialType');//51本店开通  52逆行者活动  53小店转介绍  54旧版转入客户
        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        
        //1注册时间新->旧  2 注册时间旧->新 3开通特权时间新->旧  4开通特权时间旧->新 5本店消费高->低 6本店消费低->高 
        //7服务次数多->少 8服务次数少->多 9美容金高->低 10美容金低->高 11最近消费新->旧 12最近消费旧->新 
        //13进店次数多->少 14进店次数少->多 15预警等级高->低 16预警等级低->高
        $orderType = input('param.orderType');
        $order = 'c.id desc';
        switch ($orderType) {
            case 1 :
                $order = 'c.add_time desc, c.id desc';
                break;
            case 2 :
                $order = 'c.add_time asc, c.id desc';
                break;
            case 3 :
                $order = 'c.special_time desc, c.id desc';
                break;
            case 4 :
                $order = 'c.special_time asc, c.id desc';
                break;
            case 5 :
                $order = 'cd.sell_money desc, c.id desc';
                break;
            case 6 :
                $order = 'cd.sell_money asc, c.id desc';
                break;
            case 7 :
                $order = 'cd.ser_num desc, c.id desc';
                break;
            case 8 :
                $order = 'cd.ser_num asc, c.id desc';
                break;
            case 9 :
                $order = 'c.red_money desc, c.id desc';
                break;
            case 10 :
                $order = 'c.red_money asc, c.id desc';
                break;
            case 11 :
                $order = 'cd.sell_time desc, c.id desc';
                break;
            case 12 :
                $order = 'cd.sell_time asc, c.id desc';
                break;
            case 13 :
                $order = 'cd.visit_num desc, c.id desc';
                break;
            case 14 :
                $order = 'cd.visit_num asc, c.id desc';
                break;
            case 15 :
                $order = 'c.warning_level=0 asc,c.warning_level asc, c.id desc';
                break;
            case 16 :
                $order = 'c.warning_level=0 desc,c.warning_level desc, c.id desc';
                break;
            default :
                $order = 'c.id desc';
        }

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
              $a7 = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= c.id) > 0";
              $o = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= o.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
              $orderwhere['o.staff_id'] = 0;
          }elseif ($staff_id == -3) {//活动推广
              $where['s.type'] = 8;
              $orderwhere['s.type'] = 8;
              $owhere['s.type'] = 8;
          }  else {
              $where['s.staff_id'] = $staff_id;
              $orderwhere['o.staff_id'] = $staff_id;
          }  
        } 
        $orderwhere = isset($orderwhere)?$orderwhere:[];
        $a = isset($a) ? $a : '';
        $a7 = isset($a7) ? $a7 : '';
        $o = isset($o) ? $o : '';
        $owhere = isset($owhere) ? $owhere : [];
        $where['s.store_id'] = $store_id;
        //列表   1未消费2新客户3老客户4开通特权5体验特权
        if ($type == 0) {
            $allList = StoreCtm::alias('s')
                ->join('staff st', 'st.id = s.staff_id', 'left')
                ->join('custom c','c.id = s.ctm_id','left')
                ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                ->where($where)
                ->where($a)
                //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                ->group('s.ctm_id')
                ->limit($offset, $perPage)
                ->orderRaw($order)
                ->field('s.ctm_id,st.staff_name,st.staff_on')
                ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $allList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $allList[$k]['is_medical'] = $is_medical;
                $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $allList[$k]['source_type'] = $ctmRow['source_type'];
                $allList[$k]['source_id'] = $ctmRow['source_id'];
                $allList[$k]['warning_level'] = $ctmRow['warning_level'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        //->where('pay_money', '>', 0)
                        ->where('store_id',$store_id)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $allList[$k]['pay_time'] = $pay_time;
            }
            $data = $allList;
        } elseif ($type == 1) {
            $noExpenseList = StoreCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where($where)
                    ->where($a)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('s.ctm_id,st.staff_name,st.staff_on,s.add_time')
                    ->select();
            foreach ($noExpenseList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $noExpenseList[$k]['note'] = $notename['note'];
                $noExpenseList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $noExpenseList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $noExpenseList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $noExpenseList[$k]['is_medical'] = $is_medical;
                $noExpenseList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                $noExpenseList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $noExpenseList[$k]['custom_name'] = $ctmRow['custom_name'];
                $noExpenseList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $noExpenseList[$k]['vip_status'] = $ctmRow['vip_status'];
                $noExpenseList[$k]['source_type'] = $ctmRow['source_type'];
                $noExpenseList[$k]['source_id'] = $ctmRow['source_id'];
                $noExpenseList[$k]['pay_time'] = null;
                $noExpenseList[$k]['warning_level'] = $ctmRow['warning_level'];
            }
            $data = $noExpenseList;
        }elseif ($type == 2) {
            $allList = StoreCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where($where)
                    ->where($a)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) > 0")
                    ->where("(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `store_id`=".$store_id." and `status`>1 and `ctm_id`=s.ctm_id)=0")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('s.ctm_id,st.staff_name,st.staff_on,s.add_time')
                    ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }

                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $allList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $allList[$k]['is_medical'] = $is_medical;
                $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $allList[$k]['source_type'] = $ctmRow['source_type'];
                $allList[$k]['source_id'] = $ctmRow['source_id'];
                $allList[$k]['warning_level'] = $ctmRow['warning_level'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        //->where('pay_money', '>', 0)
                        ->where('store_id',$store_id)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $allList[$k]['pay_time'] = $pay_time;
            }
            $data = $allList;
        } elseif ($type == 3) {
            $allList = Order::alias('o')
                    ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                    ->join('staff st', 'st.id = o.staff_id', 'left')
                    ->join('custom c','c.id = o.ctm_id','left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where($orderwhere)
                    ->where($o)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) = 1")
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                    ->select();
            foreach ($allList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $allList[$k]['note'] = $notename['note'];
                $allList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }

                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $allList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $allList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $allList[$k]['is_medical'] = $is_medical;
                $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $allList[$k]['custom_name'] = $ctmRow['custom_name'];
                $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $allList[$k]['vip_status'] = $ctmRow['vip_status'];
                $allList[$k]['source_type'] = $ctmRow['source_type'];
                $allList[$k]['source_id'] = $ctmRow['source_id'];
                $allList[$k]['warning_level'] = $ctmRow['warning_level'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('type','in','1,2,5')
                        //->where('pay_money', '>', 0)
                        ->where('store_id',$store_id)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $allList[$k]['pay_time'] = $pay_time;
            }
            $data = $allList;
        } elseif ($type == 4) {
            $oldList = Order::alias('o')
                    ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                    ->join('staff st', 'st.id = o.staff_id', 'left')
                    ->join('custom c','c.id = o.ctm_id','left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where($orderwhere)
                    ->where($o)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=o.ctm_id and `status`=2 and `type`in(1,2,5)) > 1")
                    ->group('o.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('o.id,o.ctm_id,o.pay_time,st.staff_name,st.staff_on')
                    ->select();
            foreach ($oldList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $oldList[$k]['note'] = $notename['note'];
                $oldList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $oldList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $oldList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $oldList[$k]['is_medical'] = $is_medical;
                $oldList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $oldList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $oldList[$k]['custom_name'] = $ctmRow['custom_name'];
                $oldList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $oldList[$k]['vip_status'] = $ctmRow['vip_status'];
                $oldList[$k]['source_type'] = $ctmRow['source_type'];
                $oldList[$k]['source_id'] = $ctmRow['source_id'];
                $oldList[$k]['warning_level'] = $ctmRow['warning_level'];
//                $pay_time = Order::where('ctm_id', $v['ctm_id'])
//                        ->where('status', 2)
//                        ->where('type','in','1,2,5')
//                        //->where('pay_money', '>', 0)
//                        ->where('store_id',$store_id)
//                        ->order('pay_time desc')
//                        ->value('pay_time');
//                $oldList[$k]['pay_time'] = $pay_time;
            }
            $data = $oldList;
        } elseif ($type == 5) {
            if ($specialType == 51) {
                $specialList = Order::alias('o')
                        ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                        ->join('staff st', 'st.id = o.staff_id', 'left')
                        ->join('custom c', 'c.id = o.ctm_id', 'left')
                        ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                        ->where('o.store_id', '=', $store_id)
                        ->where('o.status', 2)
                        ->where('o.type', 4)
                        ->where($orderwhere)
                        ->where($o)
                        ->where('o.mini_shop_id', 0)
                        ->group('o.ctm_id')
                        ->limit($offset, $perPage)
                        ->orderRaw($order)
                        ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                        ->select();
            }elseif ($specialType == 52) {
                $specialList = StoreCtm::alias('s')
                        ->join('custom c', 'c.id = s.ctm_id', 'left')
                        ->join('ctm_special cs', 'cs.ctm_id=s.ctm_id', 'left')
                        ->join('staff st', 'st.id = s.staff_id', 'left')
                        ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                        ->where('c.vip_status', 2)
                        ->where('cs.type', 3)
                        ->where($where)
                        ->where($a)
                        ->field('s.ctm_id,st.staff_name,st.staff_on')
                        ->group('s.ctm_id')
                        ->limit($offset, $perPage)
                        ->orderRaw($order)
                        ->select();
            }elseif ($specialType == 53) {
                $specialList = Order::alias('o')
                        ->join('store_ctm s','s.ctm_id = o.ctm_id and s.store_id = '.$store_id,'left')
                        ->join('staff st', 'st.id = o.staff_id', 'left')
                        ->join('custom c','c.id = o.ctm_id','left')
                        ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                        ->where('o.store_id', '=', $store_id)
                        ->where('o.status', 2)
                        ->where('o.type', 4)
                        ->where($orderwhere)
                        ->where($o)
                        ->where('o.mini_shop_id', '<>', 0)
                        ->group('o.ctm_id')
                        ->limit($offset, $perPage)
                        ->orderRaw($order)
                        ->field('o.id,o.ctm_id,st.staff_name,st.staff_on')
                        ->select();
            }elseif ($specialType == 54) {
                $specialList = StoreCtm::alias('s')
                        ->join('custom c', 'c.id = s.ctm_id', 'left')
                        ->join('staff st', 'st.id = s.staff_id', 'left')
                        ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                        ->where('c.vip_status', 2)
                        ->where('c.source', 1)
                        ->where($where)
                        ->field('s.ctm_id,st.staff_name,st.staff_on')
                        ->group('s.ctm_id')
                        ->limit($offset, $perPage)
                        ->orderRaw($order)
                        ->select();
            }
            
            foreach ($specialList as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $specialList[$k]['note'] = $notename['note'];
                $specialList[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
               //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $specialList[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $specialList[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $specialList[$k]['is_medical'] = $is_medical;
                $specialList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $specialList[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $specialList[$k]['custom_name'] = $ctmRow['custom_name'];
                $specialList[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $specialList[$k]['vip_status'] = $ctmRow['vip_status'];
                $specialList[$k]['source_type'] = $ctmRow['source_type'];
                $specialList[$k]['source_id'] = $ctmRow['source_id'];
                $specialList[$k]['warning_level'] = $ctmRow['warning_level'];
                $pay_time = Order::where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        //->where('pay_money', '>', 0)
                        ->where('store_id',$store_id)
                        ->order('pay_time desc')
                        ->value('pay_time');
                $specialList[$k]['pay_time'] = $pay_time;
            }

            $data = $specialList;
        } elseif ($type == 6) {
            $expSpecial = StoreCtm::alias('s')
                    ->join('staff st', 'st.id = s.staff_id', 'left')
                    ->join('custom c', 'c.id = s.ctm_id')
                    ->join('ctm_special cs','cs.ctm_id = c.id')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where('c.vip_status', 3)
                    ->where($where)
                    ->where($a)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('s.ctm_id,st.staff_name,st.staff_on')
                    ->select();

            foreach ($expSpecial as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $expSpecial[$k]['note'] = $notename['note'];
                $expSpecial[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $expSpecial[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $expSpecial[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $expSpecial[$k]['is_medical'] = $is_medical;
                $expSpecial[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $expSpecial[$k]['expspecial_time'] = $specialTime;
                $expSpecial[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $expSpecial[$k]['custom_name'] = $ctmRow['custom_name'];
                $expSpecial[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $expSpecial[$k]['vip_status'] = $ctmRow['vip_status'];
                $expSpecial[$k]['source_type'] = $ctmRow['source_type'];
                $expSpecial[$k]['source_id'] = $ctmRow['source_id'];
                $expSpecial[$k]['pay_time'] = null;
                $expSpecial[$k]['warning_level'] = $ctmRow['warning_level'];
            }
            $data = $expSpecial;
        }elseif ($type == 7) {
            $smallshop = Custom::alias('c')
                    ->join('store_ctm s','s.ctm_id = c.id and s.store_id = '.$store_id,'left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where('c.smallshop_storeid', $store_id)
                    ->where('c.status', 2)
                    ->where($a7)
                    ->where($owhere)
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id,c.smallshop_time')
                    ->select();

            foreach ($smallshop as $k => $v) {
                $staffId = StaffCtm::where('ctm_id', $v['ctm_id'])->value('staff_id');
                $smallshop[$k]['staff_name'] = Staff::where('id', $staffId)->value('staff_name');
                $smallshop[$k]['staff_on'] = Staff::where('id', $staffId)->value('staff_on');
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $smallshop[$k]['note'] = $notename['note'];
                $smallshop[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $smallshop[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $smallshop[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $smallshop[$k]['is_medical'] = $is_medical;
                $smallshop[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $smallshop[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $smallshop[$k]['custom_name'] = $ctmRow['custom_name'];
                $smallshop[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $smallshop[$k]['vip_status'] = $ctmRow['vip_status'];
                $smallshop[$k]['source_type'] = $ctmRow['source_type'];
                $smallshop[$k]['source_id'] = $ctmRow['source_id'];
                $smallshop[$k]['pay_time'] = null;
                $smallshop[$k]['warning_level'] = $ctmRow['warning_level'];
            }
            $data = $smallshop;
        } elseif ($type == 8) {
            $ctm_collect = CtmCollect::alias('co')
                    ->join('custom c', 'c.id = co.ctm_id', 'left')
                    ->join('store_ctm s','s.ctm_id = c.id and s.store_id = '.$store_id,'left')
                    ->join('custom_data cd','cd.ctm_id = c.id and cd.store_id='.$store_id,'left')
                    ->where('co.pro_id', $store_id)
                    ->where('co.type', 3)
                    ->where($a7)
                    ->where($owhere)
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('co.ctm_id,co.add_time collect_time')
                    ->select();

            foreach ($ctm_collect as $k => $v) {
                $staffId = StaffCtm::where('ctm_id', $v['ctm_id'])->value('staff_id');
                $ctm_collect[$k]['staff_name'] = Staff::where('id', $staffId)->value('staff_name');
                $ctm_collect[$k]['staff_on'] = Staff::where('id', $staffId)->value('staff_on');
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,warning_level')
                        ->find();
                $notename = Db::name('store_note')->where('store_id', $store_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $ctm_collect[$k]['note'] = $notename['note'];
                $ctm_collect[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $ctm_collect[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $ctm_collect[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $ctm_collect[$k]['is_medical'] = $is_medical;
                $ctm_collect[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $ctm_collect[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $ctm_collect[$k]['custom_name'] = $ctmRow['custom_name'];
                $ctm_collect[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $ctm_collect[$k]['vip_status'] = $ctmRow['vip_status'];
                $ctm_collect[$k]['source_type'] = $ctmRow['source_type'];
                $ctm_collect[$k]['source_id'] = $ctmRow['source_id'];
                $ctm_collect[$k]['pay_time'] = null;
                $ctm_collect[$k]['warning_level'] = $ctmRow['warning_level'];
            }
            $data = $ctm_collect;
        }
        foreach ($data as $k => $v){
            if($v['source_type'] == 0){
                $sourceName = '';
            }elseif ($v['source_type'] == 1) {
                $sourceName = '平台引流';
            }elseif ($v['source_type'] == 2) {
                $staffName = Staff::where('id',$v['source_id'])->value('staff_name');
                $sourceName = '美容师'.$staffName;
            }elseif ($v['source_type'] == 3) {
                $StoreRow = Store::where('id',$v['source_id'])->field('store_name,city')->find();
                $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                $sourceName = $cityName.'的'.$StoreRow['store_name'];
            }elseif ($v['source_type'] == 4) {
                $sourceName = '小店转介绍';
            }
            $data[$k]['sourceName'] = $sourceName;
            //查询用户是否在本店开通的特权
                $orderRow = Order::where('type', 4)
                        ->where('ctm_id', $v['ctm_id'])
                        ->where('status', 2)
                        ->where('store_id', $store_id)
                        ->order('id desc')
                        ->find();
                if ($orderRow) {
                    $data[$k]['is_exclusive'] = 1; //1是2否绑定的该店铺  显示 专属特权
                } else {
                    $data[$k]['is_exclusive'] = 2;
                }
                //查询续费年数
            $specialNum = CtmSpecial::where('ctm_id',$v['ctm_id'])->count();
            $data[$k]['renewYears'] = $specialNum;
        }
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 特权客户分类（筛选用）
     * author xfwed
     * date 2020.03.25
     */

    public function storeSpecialCtmType2020() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
            'type' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
            'type.require' => 'require',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id'];//0全部-1店铺分享 
        $store_id = $all['store_id'];
        $type = $all['type'];

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
              $a7 = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.id) > 0";
              $o = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= o.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
              $orderwhere['o.staff_id'] = 0;
          }  else {
              $where['s.staff_id'] = $staff_id;
              $orderwhere['o.staff_id'] = $staff_id;
          }    
        } 
        $orderwhere = isset($orderwhere)?$orderwhere:[];
        $a = isset($a) ? $a : '';
        $a7 = isset($a7) ? $a7 : '';
        $o = isset($o) ? $o : '';
        $where['s.store_id'] = $store_id;
        if($type == 5){
            $special1 = Order::alias('o')//本店开通
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where('o.type', 4)
                    ->where('o.mini_shop_id',0)
                    ->where($o)
                    ->where($orderwhere)
                    ->group('ctm_id')
                    ->count();
            $special2 = StoreCtm::alias('s')
                    ->join('custom c', 'c.id = s.ctm_id','left')
                    ->join('ctm_special cs','cs.ctm_id=s.ctm_id','left')
                    ->where('c.vip_status', 2)
                    ->where('cs.type',3)
                    ->where($where)
                    ->where($a)
                    //->where("(SELECT `type` FROM `ai_ctm_special` WHERE `ctm_id`=s.ctm_id AND `end_time`>'" . date('Y-m-d H:i:s', time()) . "' ORDER BY `add_time` DESC LIMIT 0,1)=2")
                    ->group('s.ctm_id')
                    ->count();
            $special3 = Order::alias('o')//小店转介绍
                    ->where('o.store_id', '=', $store_id)
                    ->where('o.status', 2)
                    ->where('o.type', 4)
                    ->where('o.mini_shop_id','<>',0)
                    ->where($orderwhere)
                    ->where($o)
                    ->group('ctm_id')
                    ->count();
            $special4 = StoreCtm::alias('s')//转入客户特权
                ->join('custom c', 'c.id = s.ctm_id','left')
                ->where('c.vip_status', 2)
                ->where('c.source',1)
                ->where($where)
                ->group('s.ctm_id')
                ->count();
            $data[0]['id'] = 51;
            $data[0]['name'] = '本店开通(' . $special1 . ')';
            $data[1]['id'] = 52;
            $data[1]['name'] = '逆行者活动(' . $special2. ')';
            $data[2]['id'] = 53;
            $data[2]['name'] = '小店转介绍(' . $special3 . ')';
            $data[3]['id'] = 54;
            $data[3]['name'] = '转入客户特权(' . $special4 . ')';
        }  else {
            $data = [];
        }
        
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 进店新客
     * author xfwed
     * date 2020.06.08
     */

    public function storeNewCtm() {
        $rule = [
            'store_id' => 'require',
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'staff_id.require' => '美容师id必须',
            'type.require' => '时间类型必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id'];
        $store_id = $all['store_id'];
        $type = $all['type'];//0全部1今日2本周3本月4自定义
        $startTime = input('param.startTime');
        $endTime = input('param.endTime');
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        //php获取今日开始时间戳和结束时间戳
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //php获取本周起始时间
        //$beginWeek = mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
        //$endWeek = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
        $beginWeek = time()- 60*60*24*7;
        $endWeek = time();
        //php获取本月起始时间戳和结束时间戳
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        if($type == 1){
            $where1['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginToday),date('Y-m-d H:i:s',$endToday)]];
        }elseif ($type == 4) {
            //return Utils::ajaxReturn(0, '666', date('Y-m-d H:i:s',$beginWeek).'-------------'.date('Y-m-d H:i:s',$endWeek));
            $where1['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginWeek),date('Y-m-d H:i:s',$endWeek)]];
        }elseif ($type == 0) {
            $where1['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginThismonth),date('Y-m-d H:i:s',$endThismonth)]];
        }elseif ($type == 3) {
            $where1['s.add_time'] = ['between',[$startTime,date('Y-m-d H:i:s',strtotime($endTime)+24*60*60)]];
        }

        if ($staff_id) {
          if($staff_id == -1){//逆行者活动
              $a = "(SELECT COUNT(`id`) FROM `ai_medical_address` WHERE `ctm_id`= s.ctm_id) > 0";
          }elseif ($staff_id == -2) {//店铺分享
              $where['s.staff_id'] = 0;
          }  else {
              $where['s.staff_id'] = $staff_id;
          }  
        } 
        $a = isset($a) ? $a : '';
        $where1 = isset($where1) ? $where1 : [];
        $where['s.store_id'] = $store_id;
        $ids = StoreCtm::alias('s')
            ->join('staff st', 'st.id = s.staff_id', 'left')
            ->where($where)
            ->where($a)
            //->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `staff_id`in(" . $staff_ids . ") and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0")
            ->group('s.ctm_id')
            ->order('s.add_time asc')
            ->column('s.id');
   
        $allList = StoreCtm::alias('s')
            ->join('staff st', 'st.id = s.staff_id', 'left')
            ->where($where1)
            ->where('s.id','in',$ids)    
            ->limit($offset, $perPage)
            ->order('s.add_time asc')
            ->field('s.ctm_id,st.staff_name,st.staff_on,s.add_time')
            ->select();
    //print_r(StoreCtm::getLastSql());
        foreach ($allList as $k => $v) {
            $ctmRow = Custom::where('id', $v['ctm_id'])
                    ->field('id,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id')
                    ->find();
            $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
            $allList[$k]['note'] = $notename['note'];
            $allList[$k]['note_name'] = $notename['name'];
            if ($notename['name']) {
                $ctmRow['custom_name'] = $notename['name'];
            }
            
            if($ctmRow['source_type'] == 0){
                $sourceName = '';
            }elseif ($ctmRow['source_type'] == 1) {
                $sourceName = '平台引流';
            }elseif ($ctmRow['source_type'] == 2) {
                $staffName = Staff::where('id',$ctmRow['source_id'])->value('staff_name');
                $sourceName = '美容师'.$staffName;
            }elseif ($ctmRow['source_type'] == 3) {
                $StoreRow = Store::where('id',$ctmRow['source_id'])->field('store_name,city')->find();
                $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                $sourceName = $cityName.'的'.$StoreRow['store_name'];
            }elseif ($ctmRow['source_type'] == 4) {
                $sourceName = '小店转介绍';
            }
            $allList[$k]['sourceName'] = $sourceName;
            $allList[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
            $allList[$k]['custom_phone'] = $ctmRow['custom_phone'];
            $allList[$k]['custom_name'] = $ctmRow['custom_name'];
            $allList[$k]['custom_photo'] = $ctmRow['custom_photo'];
            $allList[$k]['vip_status'] = $ctmRow['vip_status'];
            $pay_time = Order::where('ctm_id', $v['ctm_id'])
                    ->where('status', 2)
                    //->where('pay_money', '>', 0)
                    ->where('store_id',$store_id)
                    ->order('pay_time desc')
                    ->value('pay_time');
            $allList[$k]['pay_time'] = $pay_time;
        }
        
        return Utils::ajaxReturn(0, '成功', $allList);
    }
    
    /*
     * 进店顾客
     * author xfwed
     * date 2020.07.22
     */

    public function storeIntoShopCtm() {
        $rule = [
            'store_id' => 'require',
            'type' => 'require',
            'oto' => 'require',
            'ctmtype' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            'type.require' => '时间类型必须',
            'oto.require' => '线上线下必须',
            'ctmtype.require' => '用户类型必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $store_id = $all['store_id'];
        $type = $all['type'];//0本月1本日2全部3自定义4本周  0.本日 1.本周 2.本月 3.全部 4.自定义
        $oto = $all['oto'];//1线上0线下  =》 0线下1线上2全部
        $ctmtype = $all['ctmtype'];//0全部1老客2新客
        $startTime = input('param.startTime');
        $endTime = input('param.endTime');
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        //php获取今日开始时间戳和结束时间戳
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //php获取本周起始时间
        $beginWeek = mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
        $endWeek = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
        //$beginWeek = time()- 60*60*24*7;
        //$endWeek = time();
        //php获取本月起始时间戳和结束时间戳
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        if($type == 0){
            $where['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginToday),date('Y-m-d H:i:s',$endToday)]];
            $where1['s.staff_time'] = ['between',[date('Y-m-d H:i:s',$beginToday),date('Y-m-d H:i:s',$endToday)]];
        }elseif ($type == 1) {
            $where['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginWeek),date('Y-m-d H:i:s',$endWeek)]];
            $where1['s.staff_time'] = ['between',[date('Y-m-d H:i:s',$beginWeek),date('Y-m-d H:i:s',$endWeek)]];
        }elseif ($type == 2) {
            $where['s.add_time'] = ['between',[date('Y-m-d H:i:s',$beginThismonth),date('Y-m-d H:i:s',$endThismonth)]];
            $where1['s.staff_time'] = ['between',[date('Y-m-d H:i:s',$beginThismonth),date('Y-m-d H:i:s',$endThismonth)]];
        }elseif ($type == 4) {
            $where['s.add_time'] = ['between',[$startTime,date('Y-m-d H:i:s',strtotime($endTime)+24*60*60)]];
            $where1['s.staff_time'] = ['between',[$startTime,date('Y-m-d H:i:s',strtotime($endTime)+24*60*60)]];
        }
        
        if($ctmtype == 1){//1老客2新客3已服务
            $a ="(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) > 0";
        }elseif ($ctmtype == 2) {
            $a ="(SELECT COUNT(`id`) FROM `ai_order` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status`=2 and `type`in(1,2,5)) = 0";
        }//elseif ($ctmtype == 3) {
            //$a ="(SELECT COUNT(`id`) FROM `ai_yuyue` WHERE `store_id`=" . $store_id . " and `ctm_id`=s.ctm_id and `status` in(4,7,8)) > 0";
        //}

        $a = isset($a)?$a:[];
        $where = isset($where) ? $where : [];
        $where1 = isset($where1) ? $where1 : [];
        $where['s.store_id'] = $store_id;
        if($oto == 1){//线上
            $num = StoreCtm::alias('s')
                ->join('custom c', 'c.id = s.ctm_id', 'left')
                ->where($where)
                ->where($a)
                ->group('s.ctm_id')
                ->order('s.add_time asc')
                ->count();

            $allList = StoreCtm::alias('s')
                 ->join('custom c', 'c.id = s.ctm_id', 'left')
                ->where($where)  
                ->where($a)
                ->group('s.ctm_id')
                ->limit($offset, $perPage)
                ->order('s.add_time asc')
                ->field('s.ctm_id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,c.is_smallshop,s.add_time')
                ->select();
        }elseif ($oto == 0) {//线下
            $num = Yuyue::alias('s')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->where('s.store_id',$store_id)
                    ->where('s.status','in','4,7,8')
                    ->where($where1)
                    ->where($a)
                    ->group('s.ctm_id')
                    ->count();
            $allList = Yuyue::alias('s')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->where('s.store_id',$store_id)
                    ->where('s.status','in','4,7,8')
                    ->where($where1)
                    ->where($a)
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('s.staff_time desc')
                    ->field('s.ctm_id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,c.is_smallshop,s.staff_time add_time')
                    ->select();
        }elseif ($oto == 2) {//全部
            $matSql = StoreCtm::alias('s')
                 ->join('custom c', 'c.id = s.ctm_id', 'left')
                ->where($where)  
                ->where($a)
                ->group('s.ctm_id')
                ->order('s.add_time asc')
                ->field('s.ctm_id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,c.is_smallshop,s.add_time')
                ->buildSql();
            $num = Yuyue::alias('s')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->where('s.store_id',$store_id)
                    ->where('s.status','in','4,7,8')
                    ->where($where1)
                    ->where($a)
                    ->field('s.ctm_id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,c.is_smallshop,s.staff_time add_time')
                    ->union($matSql, true)
                    ->group('ctm_id')
                    ->select();
            $num = count($num);
            $allList = Yuyue::alias('s')
                    ->join('custom c','c.id = s.ctm_id','left')
                    ->where('s.store_id',$store_id)
                    ->where('s.status','in','4,7,8')
                    ->where($where1)
                    ->where($a)
                    ->group('s.ctm_id')
                    ->limit($offset, $perPage)
                    ->order('add_time desc')
                    ->field('s.ctm_id,c.custom_phone,c.custom_name,c.custom_photo,c.vip_status,c.is_smallshop,s.staff_time add_time')
                    ->union($matSql, true)
                    ->select();
        }
        
        foreach ($allList as $k => $v) {
            $ctmOrder = Order::where('ctm_id', $v['ctm_id'])
                    ->where('status', 2)
                    ->where('store_id',$store_id)
                    ->where('type','in','1,2,5')
                    ->find();
            if($ctmOrder){
                $allList[$k]['ctmtype'] = 2;
            }  else {
                $allList[$k]['ctmtype'] = 1;
            }
            //查询用户是否为逆行者
            $medicalData = MedicalAddress::where('ctm_id',$v['ctm_id'])->count();
            if($medicalData > 0){
                $is_medical = 1;
            }  else {
                $is_medical = 2;
            }
            $allList[$k]['is_medical'] = $is_medical;
        }
        
        $data['num'] = $num;
        $data['list'] = $allList;
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 我的推广顾客
     * author xfwed
     * date 2021.04.06
     * 此接口上线后 myCtm作废
     */

    public function myCtm2021() {
        $rule = [
            'staff_id' => 'require',
            'type' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'type.require' => '分类必须',
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type']; //1专属特权2推广顾客3粉丝4足迹5其他(服务完成的客户)6活动推广
        $staff_id = $all['staff_id'];
        $store_id = input('param.store_id');
        $tab = input('param.tab');//推广顾客 1未消费 2已消费  足迹  0全部 1自有客户 2转入客户   专属特权 0全部 1自有专属 2平台分配专属
        $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        $offset = ($page - 1) * $perPage;
        //排序 1注册时间新->旧 2注册时间旧->新 3开通特权时间新->旧 4开通特权时间旧->新 5服务次数多->少  6服务次数少->多 
        //7美容金高->低  8美容金低->高 9最近消费新->旧 10最近消费旧->新 11预警等级高->低 12预警等级低->高
        $orderType = input('param.orderType');
        $order = 'c.id desc';
        switch ($orderType) {
            case 1 :
                $order = 'c.add_time desc, c.id desc';
                break;
            case 2 :
                $order = 'c.add_time asc, c.id desc';
                break;
            case 3 :
                $order = 'c.special_time desc, c.id desc';
                break;
            case 4 :
                $order = 'c.special_time asc, c.id desc';
                break;
            case 5 :
                $order = 'c.ser_num desc, c.id desc';
                break;
            case 6 :
                $order = 'c.ser_num asc, c.id desc';
                break;
            case 7 :
                $order = 'c.red_money desc, c.id desc';
                break;
            case 8 :
                $order = 'c.red_money asc, c.id desc';
                break;
            case 9 :
                $order = 'c.sell_time desc, c.id desc';
                break;
            case 10 :
                $order = 'c.sell_time asc, c.id desc';
                break;
            case 11 :
                $order = 'c.warning_level=0 asc,c.warning_level asc, c.id desc';
                break;
            case 12 :
                $order = 'c.warning_level=0 desc,c.warning_level desc, c.id desc';
                break;
            default :
                $order = 'c.id desc';
        }
        
        //tab数量
        //1专属特权
        $specialNum = Custom::where('exclusive_staff_id',$staff_id)->count();
        //1专属特权  自有专属
        $ownspecialNum = Custom::where('exclusive_staff_id',$staff_id)
                ->whereNull('exclusive_time')
                ->count();
        //1专属特权  平台分配专属
        $othersspecialNum = Custom::where('exclusive_staff_id',$staff_id)
                ->whereNotNull('exclusive_time')
                ->count();
        $data['specialNum'] = $specialNum;
        $data['ownspecialNum'] = $ownspecialNum;
        $data['othersspecialNum'] = $othersspecialNum;
        //2推广顾客
        $spreadNum = Custom::where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->count();
        //2推广顾客 1未消费 
        $spreadNoExpenseNum = Custom::alias('c')
                ->where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) = 0")
                ->group('c.id')
                ->count();
        //2推广顾客 2已消费
        $spreadExpenseNum = Custom::alias('c')
                ->where('status',2)
                ->where('source_type',2)
                ->where('source_id',$staff_id)
                ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) > 0")
                ->group('c.id')
                ->count();
        $data['spreadNum'] = $spreadNum;
        $data['spreadNoExpenseNum'] = $spreadNoExpenseNum;
        $data['spreadExpenseNum'] = $spreadExpenseNum;
        //3粉丝
        $fans = CtmCollect::where('type', 2)
                ->where('pro_id', $staff_id)
                ->count();
        $data['fans'] = $fans;
        //4足迹
        $footmark = StaffCtm::where('staff_id',$staff_id)
                ->group('ctm_id')
                ->count();
        //4足迹  自有客户
        $ownfootmark = StaffCtm::where('staff_id',$staff_id)
                ->where('oldStaffId',0)
                ->group('ctm_id')
                ->count();
        //4足迹  转入客户
        $othersfootmark = StaffCtm::where('staff_id',$staff_id)
                ->where('oldStaffId','<>',0)
                ->group('ctm_id')
                ->count();
        $data['footmark'] = $footmark;
        $data['ownfootmark'] = $ownfootmark;
        $data['othersfootmark'] = $footmark-$ownfootmark;
        //5其他(服务完成的客户)
        $otherNum = Yuyue::where('staff_id',$staff_id)
                ->where('status','in','4,7,8')
                ->group('ctm_id')
                ->count();
        $data['otherNum'] = $otherNum;
        //6活动推广
        $matSql = Db::name('ctm_cun')
                ->alias('c')
                ->join('solitaire s','s.cun_id =c.cun_id','left')
                ->join('coupon co','co.id = c.cun_id','left')
                ->where('co.type',4)
                ->where('c.enjoy_type','=',2)
                ->where('c.enjoy_id',$staff_id)
                ->field('c.ctm_id')
                ->group('c.ctm_id')
                ->buildSql();
        
        $activity = Db::name('redpacket')->alias('r')
                ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                ->join('order_solitaire_detail osd','osd.id = rpc.order_id','left')
                ->join('solitaire s','s.id =osd.solitaire_id','left')
                ->where('r.type','=',7)
                ->where('rpc.type',4)
                ->where('osd.staff_id',$staff_id)
                ->field('r.ctm_id')
                ->group('r.ctm_id')
                ->union($matSql, true)
                ->select();
        $data['activity'] = count($activity);
        
        //列表  1专属特权2推广顾客3粉丝4足迹5其他(服务完成的客户)6活动推广
        if($type == 1){
            $staffspecialCtmWhere = [];
            if($tab == 1){
                $staffspecialCtmWhere['exclusive_time'] = null;
            }elseif ($tab == 2) {
                $staffspecialCtmWhere['exclusive_time'] = ['neq','not null']; //不为空
            }
            $list = Custom::alias('c')
                    ->where('exclusive_staff_id',$staff_id)
                    ->where($staffspecialCtmWhere)
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
        }elseif ($type == 2) {
            if($tab == 1){
               $list = Custom::alias('c')
                    ->where('status',2)
                    ->where('source_type',2)
                    ->where('source_id',$staff_id)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) = 0")
                    ->group('c.id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
            }elseif ($tab ==2) {
                $list = Custom::alias('c')
                    ->where('status',2)
                    ->where('source_type',2)
                    ->where('source_id',$staff_id)
                    ->where("(SELECT COUNT(`id`) FROM `ai_order` WHERE `ctm_id`=c.id and `status`=2 and `pay_money`>0) > 0")
                    ->group('c.id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
            }
            
        } elseif ($type == 3) {
            $list = CtmCollect::alias('cc')
                    ->join('custom c', 'c.id = cc.ctm_id', 'left')
                    ->where('cc.type', 2)
                    ->where('cc.pro_id', $staff_id)
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
        } elseif ($type == 4) {
            $staffCtmWhere = [];
            if ($tab == 1) {
                $staffCtmWhere['sc.oldStaffId'] = 0;
            } elseif ($tab == 2) {
                $staffCtmWhere['sc.oldStaffId'] = ['<>', 0];
            }
            $list = StaffCtm::alias('sc')
                    ->join('custom c', 'c.id=sc.ctm_id', 'left')
                    ->where('sc.staff_id', $staff_id)
                    ->where($staffCtmWhere)
                    ->group('sc.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
        }  elseif ($type == 5) {
            $list = Yuyue::alias('y')
                    ->join('custom c', 'c.id=y.ctm_id', 'left')
                    ->where('y.staff_id', $staff_id)
                    ->where('y.status', 'in', '4,7,8')
                    ->group('y.ctm_id')
                    ->limit($offset, $perPage)
                    ->orderRaw($order)
                    ->field('c.id ctm_id')
                    ->select();
        }elseif($type == 6){
            $matSql = Db::name('ctm_cun')
                ->alias('c')
                ->join('solitaire s','s.cun_id =c.cun_id','left')
                ->join('coupon co','co.id = c.cun_id','left')
                ->where('co.type',4)
                ->where('c.enjoy_type','=',2)
                ->where('c.enjoy_id',$staff_id)
                ->field('c.ctm_id,s.name')
                ->group('c.ctm_id')
                ->buildSql();
        
            $list = Db::name('redpacket')->alias('r')
                    ->join('red_packet_ctm rpc','rpc.id = r.packet_ctm_id','left')
                    ->join('order_solitaire_detail osd','osd.id = rpc.order_id','left')
                    ->join('solitaire s','s.id =osd.solitaire_id','left')
                    ->where('r.type','=',7)
                    ->where('rpc.type',4)
                    ->where('osd.staff_id',$staff_id)
                    ->field('r.ctm_id,s.name')
                    ->group('r.ctm_id')
                    ->union($matSql, true)
                    ->select();
        }

        foreach ($list as $k => $v) {
                $ctmRow = Custom::where('id', $v['ctm_id'])
                        ->field('id,source,custom_phone,custom_name,custom_photo,vip_status,is_smallshop,source_type,source_id,exclusive_time,exclusive_staff_id,warning_level')
                        ->find();
                $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $ctmRow['id'])->field('note,name')->find();
                $list[$k]['note'] = $notename['note'];
                $list[$k]['note_name'] = $notename['name'];
                if ($notename['name']) {
                    $ctmRow['custom_name'] = $notename['name'];
                }
                //用户是否关注了美容师
                $ctmCollectRow = CtmCollect::where('ctm_id', $v['ctm_id'])
                        ->where('type', 2)
                        ->where('pro_id', $staff_id)
                        ->find();
                if ($ctmCollectRow) {
                    $list[$k]['is_focus'] = 1; //1是2否关注了我
                } else {
                    $list[$k]['is_focus'] = 2;
                }
                //沟通信息
                $list[$k]['linkLastTime'] = StaffLink::where('staff_id', $staff_id)
                        ->where('ctm_id', $v['ctm_id'])
                        ->order('add_time desc')
                        ->value('add_time');

                //是否是通过美容师开通的特权
                if ($ctmRow['exclusive_staff_id'] == $staff_id) {
                    $list[$k]['special'] = 1; //1是2否通过我购买的
                } else {
                    $list[$k]['special'] = 2;
                }
                //查询用户开通特权时间
                if ($ctmRow['vip_status'] == 2 || $ctmRow['vip_status'] == 3) {
                    $specialTime = CtmSpecial::where('ctm_id', $ctmRow['id'])->order('id asc')->find();
                    $list[$k]['specialTime'] = $specialTime['add_time'];
                    if($specialTime['type'] == 3){
                        $ctmRow['vip_status'] = 4;//4逆行者特权 5i美优优特权 
                    }  else {
                        $specialOrder = Order::alias('o')
                            ->where('o.ctm_id', '=', $ctmRow['id'])
                            ->where('o.status', 2)
                            ->where('o.type', 4)
                            ->where('o.mini_shop_id','<>',0)
                            ->where('o.is_shop_open',1)    
                            ->group('ctm_id')
                            ->count();
                        if($specialOrder > 0){
                            $ctmRow['vip_status'] = 5;//4逆行者特权 5i美优优特权 
                        }
                    }
                    if($ctmRow['source'] == 1){
                        $ctmRow['vip_status'] = 6;//4逆行者特权 5i美优优特权 6转入客户特权
                    }
                } else {
                    $list[$k]['specialTime'] = NULL;
                }
                //查询用户是否为逆行者
                $medicalData = MedicalAddress::where('ctm_id',$ctmRow['id'])->count();
                if($medicalData > 0){
                    $is_medical = 1;
                }  else {
                    $is_medical = 2;
                }
                $list[$k]['is_medical'] = $is_medical;
                $list[$k]['is_smallshop'] = $ctmRow['is_smallshop'];
                
                $list[$k]['custom_phone'] = $ctmRow['custom_phone'];
                $list[$k]['custom_name'] = $ctmRow['custom_name'];
                $list[$k]['custom_photo'] = $ctmRow['custom_photo'];
                $list[$k]['vip_status'] = $ctmRow['vip_status'];
                $list[$k]['source_type'] = $ctmRow['source_type'];
                $list[$k]['source_id'] = $ctmRow['source_id'];
                $list[$k]['warning_level'] = $ctmRow['warning_level'];
                $list[$k]['exclusive_time'] = $ctmRow['exclusive_time'];
                if($type == 6){
                    $list[$k]['extendSourceName'] = $v['name'];
                }
                
                if($ctmRow['source_type'] == 0){
                    $sourceName = '';
                }elseif ($ctmRow['source_type'] == 1) {
                    $sourceName = '平台引流';
                }elseif ($ctmRow['source_type'] == 2) {
                    $staffName = Staff::where('id',$ctmRow['source_id'])->value('staff_name');
                    $sourceName = '美容师'.$staffName;
                }elseif ($ctmRow['source_type'] == 3) {
                    $StoreRow = Store::where('id',$ctmRow['source_id'])->field('store_name,city')->find();
                    $cityName = ProvCityArea::where('id',$StoreRow['city'])->value('name');
                    $sourceName = $cityName.'的'.$StoreRow['store_name'];
                }elseif ($ctmRow['source_type'] == 4) {
                    $sourceName = '小店转介绍';
                }
                $list[$k]['sourceName'] = $sourceName;
                //查询续费年数
                $specialNum = CtmSpecial::where('ctm_id',$v['ctm_id'])->count();
                $list[$k]['renewYears'] = $specialNum;
                
            }
            $data['list'] = $list;
        return Utils::ajaxReturn(0, '成功', $data);
    }
}
